package com.sgcc.ln.pmos.mobileapp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgcc.ln.pmos.common.listener.OnItemClickListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.mobileapp.bean.FileInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 文件名：HotTopicMoreAdapter
 * 作者：Turbo
 * 时间：2020-03-30 14:59
 * 蚁穴虽小，溃之千里。
 */
public class ChooseFileAdapter extends RecyclerView.Adapter {

    private List<FileInfo> mlist;
    private Context mContext;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ChooseFileAdapter(Context context) {
        mContext = context;
    }

    public void setNotifyData(List<FileInfo> list) {
        mlist = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choosefile, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, @SuppressLint("RecyclerView") int position) {
        ViewHolder holder = ((ViewHolder) viewHolder);
        if (mlist != null) {
            holder.mTvFilename.setText(mlist.get(position).getName());

            String time = new SimpleDateFormat("yyyy/MM/dd").format(new Date(mlist.get(position).getLastModified()));
            holder.mTvTime.setText(time);
            holder.mTvSize.setText(ShowLongFileSzie(mlist.get(position).getLength()));

            holder.mLlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(mlist.get(position), position);
                    }
                }
            });
        }
    }


    /****
     * 计算文件大小
     *
     * @param length
     * @return
     */
    public static String ShowLongFileSzie(Long length) {
        if (length >= 1024*1024) {
            return (length / (1024*1024)) + "MB";
        } else if (length >= 1024) {
            return (length / 1024) + "KB";
        } else if (length < 1024) {
            return length + "B";
        } else {
            return "0KB";
        }
    }

    @Override
    public int getItemCount() {
        return mlist != null ? mlist.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout mLlContent;
        private TextView mTvFilename;
        private TextView mTvTime;
        private TextView mTvSize;

        public ViewHolder(View view) {
            super(view);
            mLlContent = (LinearLayout) view.findViewById(R.id.ll_content);
            mTvFilename = (TextView) view.findViewById(R.id.tv_filename);
            mTvTime = (TextView) view.findViewById(R.id.tv_time);
            mTvSize = (TextView) view.findViewById(R.id.tv_size);
        }

    }
}

